(*
     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is CrossFPC.

     The Initial Developer of the Original Code is Simon Kissel.
     Portions created by Benjamin Rosseaux are Copyright (C) Benjamin Rosseaux.
     All Rights Reserved.

*)

unit cfoptionsdialog;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls,cfCompilerOptions, cfdirectoryedit, cfpcutils, cfpdprconvert;

type
  TOptionsDialog = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label4: TLabel;
    Image1: TImage;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    GroupBox1: TGroupBox;
    found_label: TLabel;
    install_folder: TEdit;
    Button3: TButton;
    GroupBox3: TGroupBox;
    libccheck: TCheckBox;
    debugcheck: TCheckBox;
    TabSheet2: TTabSheet;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    unit_directories: TEdit;
    include_directories: TEdit;
    object_directories: TEdit;
    TabSheet3: TTabSheet;
    GroupBox4: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    CheckBox9: TCheckBox;
    CheckBox10: TCheckBox;
    GroupBox6: TGroupBox;
    CheckBox11: TCheckBox;
    CheckBox12: TCheckBox;
    CheckBox13: TCheckBox;
    CheckBox14: TCheckBox;
    CheckBox15: TCheckBox;
    CheckBox16: TCheckBox;
    CheckBox17: TCheckBox;
    CheckBox18: TCheckBox;
    CheckBox19: TCheckBox;
    CheckBox20: TCheckBox;
    CheckBox21: TCheckBox;
    Edit1: TEdit;
    Label5: TLabel;
    Button4: TButton;
    CheckBox22: TCheckBox;
    ComboBox2: TComboBox;
    Label8: TLabel;
    Label7: TLabel;
    ComboBox1: TComboBox;
    output_directory: TEdit;
    Label6: TLabel;
    TabSheet4: TTabSheet;
    Label9: TLabel;
    EditDebuggerArguments: TEdit;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    procedure install_folderChange(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    fOptions: TProjectOptions;
    fDirEdit: TDirectoryEdit;
    
    procedure SetOptions(Value: TProjectOptions);
    procedure Init;
    Procedure Save;
    { Private declarations }
  public
    { Public declarations }
    global: PDirectoryOptions;
    property Options : TProjectOptions Read FOptions Write SetOptions;
  end;

var
  OptionsDialog: TOptionsDialog;

function SearchFolder(AHandle: THandle; const ATitle: string; ANetwork: Boolean): string;

implementation

{$R *.dfm}

uses ShellAPI, shlobj;

function SearchFolder(AHandle: THandle; const ATitle: string; ANetwork: Boolean): string;
var
  browseInfo: TBrowseInfo;
  idList:     PItemIDList;
begin
  SetLength(Result, MAX_PATH);

  browseInfo.hwndOwner      := AHandle;
  browseInfo.pidlRoot       := nil;

  browseInfo.pszDisplayName := PChar(Result);
  browseInfo.lpszTitle      := PChar(ATitle);
  browseInfo.ulFlags        := (integer(not ANetWork) * BIF_DONTGOBELOWDOMAIN) or BIF_RETURNONLYFSDIRS;
  browseInfo.lpfn           := nil;
  browseInfo.lParam         := 0;
  browseInfo.iImage         := 0;

  idList := SHBrowseForFolder(browseInfo);

  if idList <> nil then
  begin
    SHGetPathFromIDList(idList, PChar(Result));
    SetLength(Result, StrLen(PChar(Result)));
  end
  else
    Result := '';
end;


procedure TOptionsDialog.install_folderChange(Sender: TObject);
begin
  if fileexists(IncludeTrailingPathDelimiter(install_folder.text)+'bin\i386-win32\fpc.exe') then
  begin
    found_label.caption:='OK!';
    found_label.Font.color:=clWindowText;
  end
  else
  begin
    found_label.caption:='CrossFPC not found!';
    found_label.Font.color:=clRed;
  end;
end;

procedure TOptionsDialog.Button3Click(Sender: TObject);
var s:string;
begin
  s:=SearchFolder(application.handle, 'Browse for CrossFPC directory',false);
  if s<>'' then
  begin
    install_folder.text:=s;
    install_folderChange(self);
  end;
end;


procedure TOptionsDialog.SetOptions(Value: TProjectOptions);
begin
  FOptions := Value;
  Init;
end;


procedure TOptionsDialog.Init;
var i: Integer;
begin

  if Assigned(global) then
  with global^ do
  begin
    unit_directories.text := UnitDir;
    Include_directories.text := IncludeDir;
    Object_directories.text := LibraryDir;
  end;

  if Assigned(FOptions) then
  With Options do
  begin

    with Groupbox4 do
      for i:=0 to ControlCount-1 do
      if (Controls[i] is TCheckbox) then
      with (Controls[i] as TCheckbox) do
        Checked:=TSyntaxOption(Tag) in SyntaxOptions;

    with Groupbox6 do
      for i:=0 to ControlCount-1 do
      if (Controls[i] is TCheckbox) then
      with (Controls[i] as TCheckbox) do
        Checked:=TGenOption(Tag) in CodeGenOptions;

     Combobox2.ItemIndex:=Integer(FPCDialect);
     Combobox1.ItemIndex:=Integer(Optimizer);
     Edit1.Text:=CustomParameters;
(*    unit_directories.text := UnitSearchPath;
    Include_directories.text := IncludeSearchpath;
    Object_directories.text := ObjectSearchPath;
    output_directory.text := OutputDir; *) 

    EditDebuggerArguments.Text := DebuggerArguments;
  end;
end;

procedure TOptionsDialog.Save;
var i: Integer;
begin
  if Assigned(FOptions) then
  with Options do
  begin
    if Assigned(global) then
    with global^ do
    begin
      UnitDir := unit_directories.text;
      IncludeDir := Include_directories.text;
      LibraryDir := Object_directories.text;
    end;
    OutputDir := output_directory.text;
    CustomParameters := Edit1.Text;
    FPCDialect := TFPCDialect(Combobox2.ItemIndex);
    Optimizer := TOptimizer(Combobox1.ItemIndex);

    SyntaxOptions := [];
    with Groupbox4 do
    for i:=0 to ControlCount-1 do
     if Controls[i] is TCheckbox then
      with (Controls[i] as TCheckbox) do
      if Checked then
       SyntaxOptions:=SyntaxOptions + [TSyntaxOption(Tag)];

    CodeGenOptions := [];
    with Groupbox6 do
    for i:=0 to ControlCount-1 do
     if Controls[i] is TCheckbox then
      with (Controls[i] as TCheckbox) do
      if Checked then
       CodeGenOptions:=CodeGenOptions + [TGenOption(Tag)];

     DebuggerArguments := EditDebuggerArguments.Text;

  end;
end;

procedure TOptionsDialog.Edit1Change(Sender: TObject);
begin
  Options.CustomParameters:=(Sender as TEdit).Text;
end;

procedure TOptionsDialog.FormShow(Sender: TObject);
begin
  PageControl1.ActivePage:=TabSheet1;
end;

procedure TOptionsDialog.Button1Click(Sender: TObject);
begin
  Save;
{$ifdef standalonecrossfpc}
// Close;
{$endif}
end;

procedure TOptionsDialog.FormCreate(Sender: TObject);
begin
  fDirEdit := TDirectoryEdit.Create(Self);
end;

procedure TOptionsDialog.FormDestroy(Sender: TObject);
begin
  fDirEdit.Destroy;
  fDirEdit := nil;
end;

procedure TOptionsDialog.Button5Click(Sender: TObject);
begin
  fDirEdit.StartEdit(unit_directories, 'Unit Directories');
end;

procedure TOptionsDialog.Button6Click(Sender: TObject);
begin
  fDirEdit.StartEdit(include_directories, 'Include Directories');
end;

procedure TOptionsDialog.Button7Click(Sender: TObject);
begin
  fDirEdit.StartEdit(object_directories, 'Object Directories');
end;

procedure TOptionsDialog.Button2Click(Sender: TObject);
begin
{$ifdef standalonecrossfpc}
// Close;
{$endif}
end;

end.
